package stream;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * Stream中间操作常用方法
 *
 * @author <a href="mailto:mojo_jj@163.com">Mojo</a>
 */
public class Stream02 {
    public static void main(String[] args) {
        List<Apple> apples = getApples();
        //filter中间操作
        //filter(apples);
        //map(apples);
        //distinct(apples);
        //sorted(apples);
        //limit(apples);
        //skip(apples);
        flatMap(apples);

    }

    private static void flatMap(List<Apple> apples) {
        //返回另一个stream
        //apples.stream().flatMap(apple -> Arrays.stream(apple.getColour().split(","))).forEach(System.out::println);
        int sum = apples.stream().flatMapToInt(apple -> IntStream.of(apple.getWeight())).sum();
        System.out.println(sum);
        //比较不使用flatMap的泛型类型
        List<Integer> result = Stream.of(Arrays.asList(1, 3), Arrays.asList(5, 6)).flatMap(b1 -> b1.stream()).collect(Collectors.toList());
        List<List<Integer>> collect = Stream.of(Arrays.asList(1, 3), Arrays.asList(5, 6)).collect(Collectors.toList());
    }

    private static void skip(List<Apple> apples) {
        //颜色长度倒排，跳过最长的
        apples.stream()
                .sorted((a1, a2) -> a2.getColour().length() - a1.getColour().length())
                .skip(1)
                .forEach(System.out::println);
    }

    private static void limit(List<Apple> apples) {
        //apples.stream().sorted(Comparator.comparingInt(a -> a.getColour().length())).limit(3).forEach(System.out::println);
        //颜色长度最长的前三个
        apples.stream()
                .sorted((a1, a2) -> a2.getColour().length() - a1.getColour().length())
                .limit(3)
                .forEach(System.out::println);
    }

    private static void sorted(List<Apple> apples) {
        //排序，需要可比较或者传入Comparator实例
        //apples.stream().sorted(Comparator.comparingInt(Apple::getWeight)).forEach(System.out::println);
        //apples.stream().sorted(Comparator.comparing(Apple::getColour)).forEach(System.out::println);
        apples.stream().sorted(Comparator.comparingInt(a -> a.getColour().length())).forEach(System.out::println);
    }

    private static void distinct(List<Apple> apples) {
        //去重,依赖equal和hash方法
        apples.stream().distinct().forEach(System.out::println);
    }

    private static void map(List<Apple> apples) {
        //通过function接口返回指定的类型stream
        apples.stream().map(apple -> apple.getColour() + "+" + apple.getColour()).forEach(System.out::println);
    }

    private static void filter(List<Apple> apples) {
        apples.stream().filter(a -> a.getWeight() > 200).forEach(System.out::println);
    }

    private static List<Apple> getApples() {
        List<Apple> apples = new ArrayList<>();
        apples.add(new Apple("红色1", 111, true));
        apples.add(new Apple("黄色22", 222, true));
        apples.add(new Apple("蓝色333", 333, true));
        apples.add(new Apple("红色", 111, false));
        apples.add(new Apple("黄色4444", 222, false));
        apples.add(new Apple("蓝色", 333, false));
        apples.add(new Apple("粉色", 444, false));
        return apples;
    }
}
